<template>
	<view class="myNeeds">
		<view class="" style="background: #fff;">
			<NavBar title="需求订单">
				<template #right>
					<image class="topRight" src="/static/img/index/155.png" mode="widthFix"></image>
				</template>
			</NavBar>
		</view>
		<view class="top">
			<view @tap="data.obj.state = 'PENDING_ORDERS',data.obj.pageNo = 1,requstList()" :class="data.obj.state == 'PENDING_ORDERS'?'active':''">
				待付款
			</view>
			<view @tap="data.obj.state = 'OTHER_CONFIRMS',data.obj.pageNo = 1,requstList()" :class="data.obj.state == 'OTHER_CONFIRMS'?'active':''">
				待接单
			</view>
			<view @tap="data.obj.state = 'HAVE_IN_HAND',data.obj.pageNo = 1,requstList()" :class="data.obj.state == 'HAVE_IN_HAND'?'active':''">
				待完成
			</view>
			<view @tap="data.obj.state = 'COMPLETED',data.obj.pageNo = 1,requstList()" :class="data.obj.state == 'COMPLETED'?'active':''">
				待确认
			</view>

			<!-- PENDING_ORDERS 待付款
		 OTHER_CONFIRMS 待接单
		 HAVE_IN_HAND 进行中待完成
		 COMPLETED 待确认
		 EVALUATED 待评价
		 INVALID 已失效
		 CANCELLED 已职消
		 COMPLETE 已完成 -->

			<!-- <view @tap="data.obj.state = 'Y',data.obj.pageNo = 1,requstList()"
				:class="data.obj.state == 'Y'?'active':''">
				已失效
			</view> -->
			<!-- <view @tap="data.obj.state = 'Y',data.obj.pageNo = 1,requstList()"
				:class="data.obj.state == 'Y'?'active':''">
				已取消
			</view>
			<view @tap="data.obj.state = 'Y',data.obj.pageNo = 1,requstList()"
				:class="data.obj.state == 'Y'?'active':''">
				已完成
			</view> -->
		</view>

		<scroll-view @refresherrefresh="refresherrefresh" @scrolltolower="scrolltolower" :refresher-triggered="data.refresherBool" :refresher-enabled="true" scroll-y="true">
			<view class="list">
				<view @tap="handeClick1(item)" class="listItem" v-for="(item,index) in data.result.list" :key="index">
					<view class="top">
						接单时间:{{item.startTime}}
						<text>{{orderTitChange(item)}}</text>
					</view>
					<view class="goods">
						<view class="left">
							<text>{{item.sellType == 'OFFLINE'?'线下':'线上'}}</text>
							<image :src="data.httpUrl+'/app/file/viewFile?fileId='+item.attachmentIds[0]" mode="aspectFill"></image>
						</view>
						<view class="right">
							{{item.title}}
							<text>{{item.content}}</text>
							<view class="priceText">
								<text>{{item.price.split('.')[0]}}</text>
								<text>.{{item.price.split('.')[1]}}</text>
							</view>
						</view>
					</view>
					<view class="score" v-if="item.state == 'COMPLETE_OK'">
						<view class="">
							<text>分数：</text>
							<image v-for="(item1,index1) in 5" :key="index1" :src="item.scoring>=index1+1?'/static/img/index/157.png':'/static/img/index/158.png'" mode="widthFix"></image>
						</view>
					</view>

					<view class="bot">
						<text @tap.stop="handeClick({...item1,...item})" v-for="(item1,index1) in btnChange(item)" :class="item1.class" :key="index1">{{item1.n}}</text>
					</view>
				</view>
			</view>
		</scroll-view>
		<u-popup :round="20" :zIndex="9999" :show="data.show" @close="data.show = false">
			<view class="confirmOrderBottom">
				<view class="top">
					<text></text>
					支付
					<image @tap="data.show = false" src="/static/img/index/101.png" mode="widthFix"></image>
				</view>
				<view class="confirmOrderBottom_1">
					<view v-for="(item,index) in paymentChannels" :key="index" class="" @tap="data.payChannel = item.value">
						<view class="left">
							<image :src="'/static/img/index/'+item.icon+'.png'" mode="widthFix"></image>
							{{item.name}}
						</view>
						<image :src="data.payChannel == item.value?'/static/img/index/49.png':'/static/img/index/52.png'" mode="widthFix">
						</image>
					</view>
				</view>
				<view class="bot">
					<view class="left">
						<!-- 实付现金（元）
						<text>{{data.payInfo.totalPayAmount}}</text> -->
					</view>
					<view class="right" @tap="pay">
						立即支付
					</view>
				</view>
			</view>

		</u-popup>
		<u-popup :round="20" :zIndex="9999" :show="data.show1" @close="data.show1 = false">
			<view class="confirmOrderBottom">
				<view class="top">
					<text></text>
					评分
					<image @tap="data.show1 = false" src="/static/img/index/101.png" mode="widthFix"></image>
				</view>
				<view class="EVALUATED">
					分数：
					<image @tap="handeClick2(index)" v-for="(item,index) in 5" :key="index" :src="data.score>=index+1?'/static/img/index/157.png':'/static/img/index/158.png'" mode="widthFix"></image>
				</view>
				<view class="EVALUATEDbot" @tap="submitScore">
					确定
				</view>
			</view>

		</u-popup>
		<u-popup :round="20" :zIndex="9999" :show="data.show2" @close="data.show2 = false">
			<view class="confirmOrderBottom">
				<view class="top">
					<text></text>
					完成
					<image @tap="data.show2 = false" src="/static/img/index/101.png" mode="widthFix"></image>
				</view>
				<view class="list1">
					<view class="listItemImg" :key="index" v-for="(item,index) in data.attachmentIds">
						<image :src="data.httpUrl+'/app/file/viewFile?fileId='+item" mode="aspectFill"></image>
						<image @tap="data.attachmentIds.splice(index,1)" src="/static/img/ccc.png" mode="widthFix"></image>
					</view>

					<view class="zanwu" @click="uploadImg">
						<image src="/static/img/index/53.png" mode="widthFix"></image>
						上传图片
					</view>
				</view>
				<view class="EVALUATEDbot" @tap="submitVoucher">
					确定
				</view>
			</view>

		</u-popup>
		<SanshuiPaymentPassword v-if="data.show3" :onConfirm="data.onConfirm" :onCancel="()=>{data.show3 = false}" />
	</view>
</template>

<script setup lang="ts">
	import imgUpload from '@/utils/imgUpload.ts'
	import NavBar from '@/components/navBar.vue'
	import { reactive } from 'vue';
	import SanshuiPaymentPassword from '@/components/sanshui-payment-password/sanshui-payment-password.vue'
	import { paymentChannels, payment } from '@/utils/index'
	import { queryVpsOrderList, payVpsOrder, cancelVpsOrderState, updateEmpVpsOrderState, updateBossVpsOrderState, setVpsOrderState } from '@/utils/api/map'
	const data = reactive<any>({
		httpUrl: getApp().globalData.baseUrl,
		result: {},
		payChannel: paymentChannels[0].value,
		obj: { pageNo: 1, pageSize: 10, state: 'N' },
		score: 0,
		onConfirm: () => { },
		attachmentIds: []
	})
	const uploadImg = () => {
		uni.chooseImage({
			sourceType: ['camera', 'album'],
			success: (e : any) => {
				imgUpload(e.tempFiles.map((item : any) => { return { ...item, filePath: item.path } }), (e : any) => {
					data.attachmentIds = [...data.attachmentIds, ...e.map((item : any) => { return item.fileId })];
				})

			}
		})
	}
	const handeClick2 = (index) => {
		if (data.score < index + 1) {
			data.score = index + 1
		} else {
			data.score = index;
		}
	}
	const submitVoucher = () => {
		uni.showLoading({
			title: '加载中...',
			mask: true
		})
		updateEmpVpsOrderState({ data: { id: data.id, attachmentIds: data.attachmentIds.join(',') } }).then((res) => {
			if (res.code == 200) {
				uni.showToast({
					title: '操作成功!',
					icon: 'none'
				})
				data.show2 = false;
				data.result.list.map((item) => {
					if (item.id == data.id) {
						item.state = 'COMPLETED'
					}
				})
			}
		})
	}
	const refresherrefresh = () => {
		data.refresherBool = true;
		data.obj.pageNo = 1;
		requstList()
	}
	const submitScore = () => {
		uni.showLoading({
			title: '加载中...',
			mask: true
		})
		setVpsOrderState({ data: { id: data.id, score: data.score } }).then((res) => {
			if (res.code == 200) {
				uni.showToast({
					title: '打分成功!',
					icon: 'none'
				})
				data.result.list.map((item, index) => {
					if (item.id == data.id) {
						data.result.list.splice(index, 1)
					}
				})
				data.show1 = false;
			}
		})
	}
	const requstList = () => {
		uni.showLoading({
			title: '加载中...',
			mask: true
		})
		if (data.obj.pageNo == 1) {
			data.result.list = [];
		}
		console.log(data.obj)
		queryVpsOrderList({ data: data.obj }).then((res) => {
			res.data.list.map((item) => {
				item.price = item.price + ''
				if (item.price.indexOf('.') == -1) {
					item.price = item.price + '.00'
				}
				if (item.attachmentIds) {
					item.attachmentIds = item.attachmentIds.split(',')
				}
			})
			data.refresherBool = false;
			res.data.list = [...data.result.list, ...res.data.list];
			data.result = res.data;
			uni.hideLoading()
		})
	}
	const scrolltolower = () => {
		if (data.result.total != data.result.list.length) {
			data.obj.pageNo += 1;
			requstList()
		}
	}
	const navTo = (url) => {
		uni.navigateTo({
			url
		})
	}
	const handeClick1 = (item) => {
		uni.navigateTo({
			url: '/pages/requirementDetails1/index?id=' + item.id
		})
	}
	const handeClick = (item) => {
		if (item.n == '完成' && item.orderType == 'RECIPIENT') {
			console.log(item, 'item')
			data.show2 = true;
			data.attachmentIds = [];
			data.id = item.id;
			// updateEmpVpsOrderState
			return;
		}
		if (item.n == '取消' || item.n == '完成' || item.n == '付款') {
			if (item.n == '取消' || item.n == '完成') {
				uni.showModal({
					title: '提示',
					content: `是否${item.n}订单?`,
					success: (e) => {
						if (e.confirm) {
							uni.showLoading({
								title: '加载中...',
								mask: true
							})
							let b = null
							if (item.n == '取消') {
								b = cancelVpsOrderState
							} else {
								b = updateBossVpsOrderState
							}

							b({ data: { id: item.id } }).then((res) => {
								if (res.code == 200) {
									uni.showToast({
										title: '操作成功!',
										icon: 'none'
									})
									data.result.list.map((item1) => {
										if (item1.id == item.id) {
											if (item.n == '取消') {
												item1.state = 'CANCELLED'
											} else {
												item1.state = item1.orderType == 'RECIPIENT' ? 'COMPLETED' : 'EVALUATED'
											}
										}
									})
								}
							})
						}
					}
				})
			} else {
				data.payInfo = item;
				data.show = true;
			}
		} else {
			data.id = item.id;
			data.score = 0;
			data.show1 = true
			// uni.navigateTo({
			// 	url:'/pages/requirementDetails1/index?id='+item.id
			// })
		}
	}
	const pay = () => {
		data.show = false;
		if (data.payChannel == 'WALLET') {
			data.show3 = true;
			data.onConfirm = (e) => {
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				payVpsOrder({ data: { orderNo: data.payInfo.orderNo, payChannel: data.payChannel, payPwd: e } }).then((res) => {
					if (res.code == 200) {
						payment(data.payChannel, res.data).then((res) => {
							data.result.list.map((item) => {
								if (item.id == data.payInfo.id) {
									item.state = 'HAVE_IN_HAND'
								}
							})
						})
						uni.hideLoading()
					}
				})
			}
			return
		}
		uni.showLoading({
			title: '加载中...',
			mask: true
		})
		payVpsOrder({ data: { orderNo: data.payInfo.orderNo, payChannel: data.payChannel } }).then((res) => {
			if (res.code == 200) {
				payment(data.payChannel, res.data).then((res) => {
					data.result.list.map((item) => {
						if (item.id == data.payInfo.id) {
							item.state = 'HAVE_IN_HAND'
						}
					})
				})
				uni.hideLoading()
			}
		})
	}
	const orderTitChange = (item) => {
		//RECIPIENT 接单 //PUBLISHER发布
		if (item.orderType == 'RECIPIENT') {
			return item.state == 'PENDING_ORDERS' ? ' 等待对方付款' : item.state == 'HAVE_IN_HAND' ? '进行中待完成' : item.state == 'COMPLETED' ? '已完成待对方确认' : item.state == 'EVALUATED' ? '待评价' : item.state == 'CANCELLED' ? '已取消' : '已完成'
		} else {
			return item.state == 'PENDING_ORDERS' ? ' 待付款' : item.state == 'HAVE_IN_HAND' ? '进行中待完成' : item.state == 'COMPLETED' ? '待确认' : item.state == 'EVALUATED' ? '待评价' : item.state == 'CANCELLED' ? '已取消' : '已完成'
		}
	}
	const btnChange = (item) => {
		//RECIPIENT 接单 //PUBLISHER发布
		const arr = [];
		if (item.orderType == 'RECIPIENT') {
			if (item.state == 'PENDING_ORDERS') {
				arr.push({ n: '取消', class: 'cancel' })
			} else if (item.state == 'HAVE_IN_HAND') {
				arr.push({ n: '完成', class: 'other' })
			}
		} else {
			if (item.state == 'PENDING_ORDERS') {
				arr.push({ n: '取消', class: 'cancel' })
				arr.push({ n: '付款', class: 'other' })
			} else if (item.state == 'COMPLETED') {
				arr.push({ n: '完成', class: 'other' })
			} else if (item.state == 'EVALUATED') {
				arr.push({ n: '评价打分', class: 'other' })
			}
		}
		return arr;
	}
	requstList()
</script>

<style lang="scss">
	.list1 {
		display: flex;
		margin-top: 30rpx;
		flex-wrap: wrap;

		.listItemImg {
			width: 128rpx;
			height: 128rpx;
			border-radius: 16rpx;
			margin-right: 16rpx;
			position: relative;

			>image:nth-of-type(1) {
				width: 128rpx;
				height: 128rpx;
				border-radius: 16rpx;
			}

			>image:nth-of-type(2) {
				width: 40rpx;
				position: absolute;
				right: -10rpx;
				top: -10rpx;
				z-index: 9;
			}
		}

		.zanwu {
			margin-right: 16rpx;
			width: 128rpx;
			height: 128rpx;
			border-radius: 16rpx;
			background: #F4F6FA;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			color: #C4C4C4;
			font-size: 15rpx;

			image {
				width: 46rpx;
				margin-bottom: 5rpx;
			}
		}
	}

	.confirmOrderBottom {
		padding: 24rpx 43rpx;

		.top {
			display: flex;
			align-items: center;
			justify-content: space-between;

			image {
				width: 30rpx;
			}

			text {
				width: 30rpx;
			}

			font-size: 32rpx;
		}

		.bot {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 30rpx;

			.left {
				display: flex;
				flex-direction: column;
				font-size: 24rpx;

				text {
					margin-top: 10rpx;
					color: #FF263F;
				}
			}

			.right {
				width: 190rpx;
				line-height: 80rpx;
				background: #FF263F;
				text-align: center;
				border-radius: 80rpx;
				color: #fff;
			}
		}
	}

	.confirmOrderBottom_1 {
		>view {
			justify-content: space-between;
			// padding: 37rpx 35rpx;
			background: #fff;
			display: flex;
			align-items: center;
			margin-top: 32rpx;

			.left {
				display: flex;
				align-items: center;

				image {
					width: 48rpx;
					margin-right: 25rpx;
				}

				font-size: 24rpx;
			}

			>image {
				width: 36rpx;
			}
		}
	}

	.myNeeds {
		height: 100vh;
		background: #F4F6FA;
		display: flex;
		flex-direction: column;

		scroll-view {
			padding: 24rpx;
			flex: 1;
			overflow: auto;
		}

		.list {
			>view:nth-of-type(1) {
				margin-top: 0;
			}
		}

		.listItem {
			padding: 0 20rpx;
			margin-top: 24rpx;
			background: #fff;
			border-radius: 16rpx;

			>.top {
				display: flex;
				align-items: center;
				justify-content: space-between;
				color: #595959;

				text {
					color: #FF8C00;
				}

				padding: 20rpx 0;
			}

			>.goods {
				display: flex;
				align-items: center;

				.left {
					position: relative;
					width: 140rpx;
					height: 140rpx;
					border-radius: 24rpx;
					background: #ccc;
					margin-right: 16rpx;

					image {
						border-radius: 24rpx;
						width: 100%;
						height: 100%;
					}

					text {
						width: 72rpx;
						line-height: 40rpx;
						text-align: center;
						font-size: 20rpx;
						color: #FFF5E2;
						display: inline-block;
						position: absolute;
						left: 0;
						top: 0;
						box-shadow: inset -1rpx -1rpx 4rpx 0rpx rgba(255, 234, 160, 0.3);
						border-radius: 24rpx 0 24rpx 0;
						background: linear-gradient(223deg, #CD9A67 0%, #C69769 100%);
					}
				}

				.right {
					flex: 1;
					width: 0;
					font-size: 32rpx;
					display: flex;
					flex-direction: column;

					>text {
						color: #595959;
						font-size: 24rpx;
						margin-top: 8rpx;
					}
				}
			}

			>.bot {
				display: flex;
				justify-content: flex-end;
				padding: 24rpx 0;

				.cancel {
					width: 140rpx;
					line-height: 64rpx;
					text-align: center;
					border-radius: 32rpx;
					margin-left: 16rpx;
					color: #1313168c;
					border: 2rpx solid #1313168c;
				}

				.other {
					width: 140rpx;
					line-height: 64rpx;
					text-align: center;
					border-radius: 32rpx;
					margin-left: 16rpx;
					color: #6B64D3;
					border: 2rpx solid #6B64D3;
				}
			}
		}

		.score {
			margin-top: 24rpx;
			border-top: 2rpx solid #F4F6FA;

			view {
				display: flex;
				align-items: center;
				margin-top: 16rpx;

				text {
					// width: 0rpx;
				}

				image {
					margin-left: 12rpx;
					width: 36rpx;
				}
			}
		}

		>.top {
			background: #fff;
			display: flex;
			justify-content: space-between;
			padding: 38rpx 180rpx 0 180rpx;

			view {
				color: #595959;
				display: flex;
				flex-direction: column;
				align-items: center;

				&::after {
					margin-top: 20rpx;
					height: 4rpx;
					border-radius: 4rpx;
					width: 40rpx;
					content: '';
					display: inline-block;
				}
			}

			.active {
				color: #6B64D3;

				&:after {
					background: #6B64D3;
				}
			}
		}

		>.bot {
			padding: 8rpx 32rpx 20rpx 32rpx;
			background: #fff;

			text {
				display: inline-block;
				width: 100%;
				line-height: 80rpx;
				text-align: center;
				font-size: 32rpx;
				color: #fff;
				border-radius: 80rpx;
				background: #6B64D3;
			}
		}
	}

	.topRight {
		width: 48rpx;
	}

	.EVALUATED {
		display: flex;
		align-items: center;
		margin-top: 20rpx;

		image {
			width: 50rpx;
		}
	}

	.EVALUATEDbot {
		background: #6B64D3;
		width: 100%;
		margin: 0 auto;
		line-height: 88rpx;
		border-radius: 88rpx;
		text-align: center;
		color: #fff;
		margin-top: 50rpx;
	}
</style>